﻿using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.WindAlert;
using Nop.Services.WindAlert;
using Nop.Web.Framework.Security;
using Nop.Web.WindAlert.Extensions;
using System.Linq;
using System.Web.Mvc;

namespace Nop.Web.Controllers
{
    public partial class WCameraController : BasePublicController
    {
        #region Fields

        private readonly ICameraService _cameraService;
        private readonly IWorkContext _workContext;
        private readonly WindAlertSettings _windAlertSettings;
        #endregion

        #region Constructors

        public WCameraController(IWorkContext workContext, ICameraService CameraService, WindAlertSettings windalertSettings)
        {
            this._workContext = workContext;
            this._cameraService = CameraService;
            this._windAlertSettings = windalertSettings;
        }

        #endregion

        #region Utilities

        [NonAction]
        protected bool IsCurrentUserRegistered()
        {
            return _workContext.CurrentCustomer.IsRegistered();
        }

        #endregion

        public ActionResult Index(int? productThumbPictureSize)
        {
            if (!IsCurrentUserRegistered() && _windAlertSettings.BlockCameras)
                ViewData["IsRegistered"] = false;
            else
                ViewData["IsRegistered"] = true;

            var listCameras = _cameraService.GetAllCameras(false)
                .Select(x =>
                {
                    var camModel = x.ToModel();
                    return camModel;
                })
                .ToList();

            return View(listCameras);
        }

        public ActionResult HomePageCameras()
        {
            var listCameras = _cameraService.GetAllCameras(false)
                .Select(x =>
                {
                    var camModel = x.ToModel();
                    return camModel;
                })
                .ToList();

            return PartialView(listCameras);
        }


        [NopHttpsRequirement(SslRequirement.No)]
        public ActionResult Camera(int cameraId)
        {
            var camera = _cameraService.GetCameraById(cameraId );
            if (camera == null || camera.Deleted)
                return RedirectToRoute("HomePage");

            var model = camera.ToModel();

            if (!IsCurrentUserRegistered() && _windAlertSettings.BlockCameras)
                ViewData["IsRegistered"] = false;
            else
                ViewData["IsRegistered"] = true;

            return View(model);
        }

    }
}
